<?
/************************************************************************/
/* 	The author of this program code:                                    */
/*  Dmitry Sheiko (sheiko@cmsdevelopment.com)	                    	*/
/* 	Copyright by Dmitry Sheiko											*/
/* 	http://www.cmsdevelopment.com     									*/
/************************************************************************/

/**
	* @return void
	* @param string $limit
	* @param string $addparams
	* @desc Information channel: storyline, articles, custom list 
	**/

function get_calendar ($arg_name="news", $addparams=false) {


	global $http_path, $env, $sapi_obj;

	if( preg_match("/_BASEURL\(/s", $addparams) ) $BASEURL =  preg_replace("/^.*?_BASEURL\((.*?)\)_.*?$/s", "\\1", $addparams);
	if($BASEURL) $baseurl = $BASEURL; else $baseurl=$sapi_obj->env["argv_string"];
	$baseurl = $baseurl."/";
	if( @preg_match("/^\//", $baseurl) ) $baseurl = $sapi_obj->env["http_path"].substr($baseurl, 1);
	elseif( !@preg_match("/^http:/", $baseurl) ) $baseurl = $sapi_obj->env["http_path"].$baseurl;

	$arg=$sapi_obj->env["argv_nav"][0];
	$current_time=($sapi_obj->env["argv_nav"][1]?$sapi_obj->env["argv_nav"][1]:time());

	if(!$arg) $time_of_first_day = mktime(0,0,0,date("m"), 1, date("Y"));
	switch($arg) {
		case "month-1":
		$current_time = mktime( date("H",$current_time) , date("i",$current_time), date("s",$current_time), date("m",$current_time)-1, date("d", $current_time), date("Y", $current_time));
		break;
		case "month+1":
		$current_time = mktime(date("H",$current_time),date("i",$current_time),date("s",$current_time),date("m",$current_time)+1, date("d",$current_time), date("Y", $current_time));
		break;
		case "year-1":
		$current_time = mktime( date("H",$current_time) , date("i",$current_time), date("s",$current_time), date("m",$current_time), date("d", $current_time), date("Y", $current_time)-1);
		break;
		case "year+1":
		$current_time = mktime( date("H",$current_time) , date("i",$current_time), date("s",$current_time), date("m",$current_time), date("d", $current_time), date("Y", $current_time)+1);
		break;
	}


	if($sapi_obj->env["argv_virtual.month"] AND $sapi_obj->env["argv_virtual.year"])
	$current_time = $time_of_first_day = mktime(0,0,0, $sapi_obj->env["argv_virtual.month"], $sapi_obj->env["dhtml_virtual_section_day"], $sapi_obj->env["argv_virtual.year"]);
	$time_of_first_day = mktime(date("H",$current_time),date("i",$current_time),date("s",$current_time),date("m",$current_time), 1, date("Y", $current_time));



	$sapi_obj->$sapi_obj->env["day_time"]=$time_of_first_day;
	$sapi_obj->env["day_current_date"]  = $current_time;
	$cur_year=date("Y", $time_of_first_day);
	$cur_month=date("m", $time_of_first_day);
	//		$cur_day=date("d", $time_of_first_day);

	$filename=ROOT_PATH . "usr/xml/vdb/".$arg_name.".xml";


	$db = new sapi_vdb($filename);
	$news=$db->get_data();

	if(is_array($news))foreach($news as $val) {

		// Ordered by system date

		$arr=array();preg_match('/^(\d{4})-(\d{2})-(\d{2})\s(.*?)$/',$val['DATE_CREATE'],$arr);
		$urls[(int)$arr[1]][(int)$arr[2]][(int)$arr[3]]=$arr[1]."/".$arr[2]."/".$arr[3];

		// Ordered by custom DATE
		/*
		$arr=array();preg_match('/^(\d{4})\/(\d{2})\/(\d{2})$/',$val['DATE'],$arr);
		$urls[(int)$arr[1]][(int)$arr[2]][(int)$arr[3]]=$arr[1]."/".$arr[2]."/".$arr[3];
		*/

	}

		$w=date("w", $time_of_first_day);
	$curday=date("d");
	$curmonth=date("m");
	$curyear=date("Y");
	$sapi_obj->env["curdate"] = date("F Y", $current_time);
	
	if(!$w) $w=7;	for($i=1;$i<=$w;$i++) $sapi_obj->env["day"][$i]="&nbsp;";
	$sapi_obj->env["day"][$w]="1";
	for($i=1+$w;$i<=38;$i++) {
		$sapi_obj->env["day"][$i]=date("j", mktime(0,0,0,date("m",$current_time), $i-$w+1, date("Y",$current_time)));
		if ( $sapi_obj->env["day"][$i]<=1 ) $flag=true;
		if($flag) $sapi_obj->env["day"][$i]="&nbsp;";
	}

	/*
	CLASS: 	day selected today
	day unselectable
	*/
	$base_url = $baseurl;
	for($i=1;$i<=39;$i++) {

		$sapi_obj->env["day_href.".$i]=" ";
		if($sapi_obj->env["day"][$i]=="&nbsp;") $sapi_obj->env["day_class.".$i]="emptycell";
		elseif(	$urls[(int)$cur_year][(int)$cur_month][(int)$sapi_obj->env["day"][$i]] ) {
			$sapi_obj->env["day_href.".$i]=" onclick=\"location.href='".$base_url.$urls[(int)$cur_year][(int)$cur_month][(int)$sapi_obj->env["day"][$i]]."/".($arg?"data/".$arg."/":"")."';\""; $sapi_obj->env["day_class.".$i]="day selected";	}
			else $sapi_obj->env["day_class.".$i]="day";

			$sapi_obj->env["day_href.$i"].=' onMouseOver="this.style.backgroundColor=\'#dbdbdb\';" onMouseOut="this.style.backgroundColor=\'\';" ';
			if((int)$sapi_obj->env["day"][$i]==(int)$sapi_obj->env["dhtml_virtual_section_day"] AND $cur_month==$sapi_obj->env["dhtml_virtual_section_month"] AND $cur_year==$sapi_obj->env["dhtml_virtual_section_year"]) { $sapi_obj->env["day_class.".$i]=str_replace("selected","", $sapi_obj->env["day_class.".$i]); $sapi_obj->env["day_class.".$i].=" active"; }
			if($sapi_obj->env["day"][$i]==$curday AND $cur_month==$curmonth AND $cur_year==$curyear) $sapi_obj->env["day_class.$i"].=" today";
			if(
			$i==6 OR $i==7 OR $i==13 OR $i==14 OR $i==20 OR $i==21 OR $i==27 OR $i==28 OR $i==34 OR $i==35
			) $sapi_obj->env["day_class.".$i].=" weekend";
			$sapi_obj->env["day.$i"] = $sapi_obj->env["day"][$i];
	}
	unset($sapi_obj->env["day"]);
	return array("1"=>array("success"=>1));
}

?>